<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('任务调度列表')" />
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>仓库类型：</label>
                            <select id="dpRepoType" name="dpRepoType" th:with="type=${@dict.getType('repo_type')}">
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <label>调度名称：</label>
                            <input type="text" name="dpName"/>
                        </li>
                        <li>
                            <label>调度地址：</label>
                            <input type="text" name="dpPath"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="addRepo()" shiro:hasPermission="busi:dispatcher:add">
                <i class="fa fa-plus"></i> 添加
            </a>
            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="busi:dispatcher:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
            <a class="btn btn-primary" onclick="javascript:cron()">
                <i class="fa fa-code"></i> 生成表达式
            </a>
                <a class="btn btn-success multiple disabled" onclick="executeNow()" shiro:hasPermission="busi:dispatcher:launch">
                <i class="fa fa-plus"></i> 执行一次
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('busi:dispatcher:edit')}]];
    var removeFlag = [[${@permission.hasPermi('busi:dispatcher:remove')}]];
    var datas = [[${@dict.getType('repo_type')}]];
    var warn_level = [[${@dict.getType('warn_level')}]];
    var prefix = ctx + "busi/dispatcher";
    var ktExeStatus = [[${@dict.getType('kt_exe_status')}]];

    function addRepo() {
        $.operate.add($("#dpRepoType").val())
    }


    $(function() {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add/{id}",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "任务调度",
            sortName: "createdTime",
            sortOrder: "DESC",
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: 'ID',
                    visible: false
                },
                {
                    field: 'dpName',
                    title: '调度名称'
                },
                {
                    field: 'dpTypeName',
                    title: '调度类型'
                },
                {
                    field: 'dpPath',
                    title: '调度地址'
                },
                {
                    field: 'dpRepoType',
                    title: '资源库类型',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(datas, row.dpRepoType);
                    }
                },
                {
                    field: 'repoName',
                    title: '资源库名称'
                },
                {
                    field: 'paramsGroupName',
                    title: '参数组'
                },
                {
                    field: 'warnMsgIds',
                    title: '预警'
                },
                {
                    field: 'warnLevel',
                    title: '预警等级',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(warn_level, row.warnLevel);
                    }
                },
                {
                    field: 'currentStatus',
                    title: '当前任务状态',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(ktExeStatus, row.currentStatus);
                    }
                },
                {
                    field: 'createdTime',
                    title: '创建时间'
                },
                {
                    field: 'status',
                    title: '启用/停用',
                    formatter: function (value, row, index) {
                        return statusTools(row);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        // actions.push('<a class="btn btn-info btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="executeNow(\'' + row.id +'\',\'' + row.status + '\')"><i class="fa fa-play"></i> 执行一次 </a> ');
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="exeLog(\'' + row.id + '\')"><i class="fa fa-edit"></i>日志记录</a> ');
                        actions.push('<a class="btn btn-info btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="addJob(\'' + row.id + '\')"><i class="fa fa-edit"></i>添加定时任务</a> ');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);

        $("#dpRepoType").bind("change", function () {
            $.table.refreshOptions(options);
        });
    });

    function executeNow(id, status) {
        table.set();
        var rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录");
            return;
        }
        $.modal.confirm("确认执行选中的" + rows.length + "条任务调度吗?", function() {
            var url = prefix + "/launchF";
            var data = { "ids": rows.join() };
            $.operate.submit(url, "post", "json", data, function (resp) {
                $.operate.ajaxSuccess(resp);
            });
        });
    }

    /* 用户状态显示 */
    function statusTools(row) {
        if (row.status == 1) {
            return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="enable(\'' + row.id + '\')"></i> ';
        } else {
            return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="disable(\'' + row.id + '\')"></i> ';
        }
    }

    /* 用户管理-停用 */
    function disable(id) {
        $.modal.confirm("确认要停用此调度吗？", function() {
            $.operate.post(prefix + "/changeStatus", { "id": id, "status": 1 });
        })
    }

    /* 用户管理启用 */
    function enable(id) {
        $.modal.confirm("确认要启用此调度吗？", function() {
            $.operate.post(prefix + "/changeStatus", { "id": id, "status": 0 });
        })
    }

    function exeLog(id) {
        $.modal.openTab("执行日志("+id+")", "/busi/executionLog/"+id);
    }

    function addJob(id) {
        $.modal.open("添加定时任务", "/busi/job/add/"+id, null, null,  function(index, layero) {
            var iframeWin = layero.find('iframe')[0];
            iframeWin.contentWindow.submitHandler(index, layero);
            $.modal.openTab("定时任务("+id+")", "/busi/job/"+id);
        });
    }

    /* cron表达式生成 */
    function cron() {
        var url = ctx + "busi/job/cron";
        var height = $(window).height() - 50;
        top.layer.open({
            maxmin: true,
            title: "Cron表达式生成器",
            type: 2,
            area: ['800px', height + "px" ], //宽高
            shadeClose: true,
            content: url
        });
    }

    function submitHandler() {
        var rows = $.table.selectColumns("id");
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录");
            return;
        }
        $.modal.close();
        return rows.join();
    }
</script>
</body>
</html>